<?php
require('../includes/init.php');


$res_arr = array(
		'type' => '',
		'msg'  => '',
		'url'  => ''
	);

$v = trim($_REQUEST['v']);
$user = trim($_REQUEST['user']);

if($_SERVER['REQUEST_METHOD'] == 'POST' && $_POST['act'] == 'post')
{
	$password = trim($_POST['pwd']);
	$captcha  = $_POST['imgcode'];	
	
	if($password=='' || strlen($password)<6 || strlen($password)>20)
	{
		$res_arr = array(
			'msg' => '6-20个字符，中文算2个字符',
			'err_id' => 'err_pwd'
		);
		die(json_encode($res_arr));
	}
	
	//验证码
	require(ROOT_PATH . '/includes/securimage/securimage.php');
    $securimage = new Securimage();
    if ($securimage->check($captcha) == false) {
    	$res_arr = array(
			'type' => 'err_code',
			'msg' => '验证码错误'
		);
		die(json_encode($res_arr));
    }
	
			//密码更新到数据库
			$sql = "UPDATE `user` SET `password`='".md5($password)."',`pwd`='".encrypt($password)."' WHERE `forget_pwd`='".$v."' AND `username`='".$user."';";
			$res = $db->query($sql);
			
			$return_url = "//".$_SERVER['HTTP_HOST'];
			if($res ){
				$res_arr = array(
					'type' => 'ok',
					'msg' => '完成新密码设置',
					'url' => $return_url
				);
				die(json_encode($res_arr));
			}else {
				$res_arr = array(
					'type' => 'err_pwd',
					'msg' => '重置错误！请重试或重新忘记密码操作'
				);
				die(json_encode($res_arr));
				}

}
?>
<?php include('../new/header.php');?>
<link href="/css/v1/register.css" rel="stylesheet" type="text/css" />
<script src="/js/jquery-1.7.1.min.js" type="text/javascript"></script>
<script type="text/javascript" src="/js/jquery.form.js"></script>
<script type="text/javascript" src="/js/artDialog4.1.7/artDialog.js?skin=blue"></script>
<style>
    .login_wrap{
        height: 650px;
    }
</style>
<div id="content_body">
  <div class="login_wrap">
    <div class="fm960">
      <div class="logo"> <a href="/" class="mogujie fl" title="<?php echo $cfg_site['web_title'];?>"><?php echo $cfg_site['web_title'];?></a> </div>
      <div class="content">
        <div class="lg_left">
          <h1>重置密码</h1>
          <div class="lg_form">
            <form id="myform" action="" method="post">
              <div class="lg_name"> <span>新密码：</span>
                <input type="text" placeholder="请输入新密码" value="" name="pwd" id="pwd" class="text r3" maxlength="32">
              </div>
              <div class="err_name"><span>请输入新密码</span></div>
              <div class="clear"></div>
              <div class="clear" id="appendimgcheck"></div>
              <div class="lg_chk shouldremove"> <span>验证码：</span>
                <div style="float:left">
                <input id="imgcode" type="text" maxlength="4" name="imgcode" class="text r3" style="border-color: rgb(207, 207, 207);" onkeyup="this.value = this.value.toUpperCase();">
                </div>
                <img id="siimage" style="border: 1px solid #000; margin-right: 15px; margin-left:4px; margin-top:1px; cursor:pointer;" src="../includes/securimage/securimage_show.php?sid=<?php echo md5(uniqid()) ?>" onclick="this.src='../includes/securimage/securimage_show.php?sid='+Math.random()" alt="看不清？换一个" title="看不清？换一个" height="30" align="left" />
             <!--<div style="float:left; width:32px; height:32px; cursor:pointer;">
             <object type="application/x-shockwave-flash" data="../includes/securimage/securimage_play.swf?bgcol=#ffffff&amp;icon_file=../includes/securimage/images/audio_icon.png&amp;audio_file=../includes/securimage/securimage_play.php" height="32" width="32">
            <param name="movie" value="../includes/securimage/securimage_play.swf?bgcol=#ffffff&amp;icon_file=../includes/securimage/images/audio_icon.png&amp;audio_file=../includes/securimage/securimage_play.php" />
            </object>
            </div>-->
            <a tabindex="-1" style="border-style: none;" href="#" title="看不清？换一个" onclick="document.getElementById('siimage').src = '../includes/securimage/securimage_show.php?sid=' + Math.random(); this.blur(); return false"><img src="../includes/securimage/images/refresh.png" alt="Reload Image" height="32" width="32" onclick="this.blur()" align="bottom" border="0" /></a>
              </div>
              <div class="clear"></div>
              <div class="lg_login">
                <input type="submit" value=" 提交 ">
              </div>
              <input name="act" value="post" type="hidden">
              <input name="v" value="<?php echo $v?>" type="hidden">
              <input name="user" value="<?php echo $user?>" type="hidden">
            </form>
          </div>
        </div>
        <div class="lg_right">
          <h2>注册</h2>
          <span style="margin-left:40px;">还没有账号？</span> <a style="margin-left:40px;" class="reg" href="register.php"></a> </div>
        <input type="hidden" value="" id="isban">
      </div>
      <p class="copyright" title="<?php echo $cfg_site['web_title'];?>">©Copyright 2005-<?php echo date('Y');?> <?php echo $cfg_site['web_title'];?></p>
    </div>
  </div>
</div>
<script type="text/javascript">
$(document).ready(function() {
	var options = {
		dataType:  'json',
        beforeSubmit:  validate,
        success:       showResponse
    }; 
 
    $('#myform').ajaxForm(options);
});

function validate(formData, jqForm, options)
{
	if ($('#pwd').val() == '')
	{
		art.dialog({
			id: 'memberLoginMsg',
			title: '错误提示',
			content: '新密码不能为空！',
			lock: true,
			opacity: 0.3,
			ok:function(){
				$('#pwd').focus();
			}
		});
        return false; 
    }	
		
	if ($('#imgcode').val().length != 4)
	{
		art.dialog({
			id: 'memberLoginMsg',
			title: '错误提示',
			content: '验证码必须是4位有效的数字或字符！',
			lock: true,
			opacity: 0.3,
			ok:function(){
				$('#imgcode').focus();
			}
		});
        return false; 
    }
	
	return true;
}

function showResponse(data)
{
	if(data.type == 'ok')
	{
		art.dialog({
			id: 'memberLoginMsg',
			title: '系统提示',
			content: data.msg,
			lock: true,
			opacity: 0.3,
			close: function () {
    			window.location = "/new/index.php";
    		}
		}).time(1);
		return;
	}
	else
	{
		art.dialog({
			id: 'memberLoginMsg',
			title: '错误提示',
			content: data.msg,
			lock: true,
			opacity: 0.3,
			ok:function(){				
				switch (data.type)
				{
					case 'err_pwd':
						break;
					default:
						break;
				};
			}//ok
		});
        return false; 
	}
}
</script>
<?php include('../new/footer.php');?>
